package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.PasswordConstant;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.EmployeeDTO;
import com.sky.dto.EmployeeLoginDTO;
import com.sky.dto.EmployeePageQueryDTO;
import com.sky.entity.Employee;
import com.sky.exception.AccountLockedException;
import com.sky.exception.AccountNotFoundException;
import com.sky.exception.PasswordErrorException;
import com.sky.mapper.EmployeeMapper;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.EmployeeService;
import com.sky.utils.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@Service
public class EmployeeServiceImpl implements EmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;

    /**
     * 员工登录
     *
     * @param employeeLoginDTO
     * @return
     */
    public Employee login(EmployeeLoginDTO employeeLoginDTO) {
        String username = employeeLoginDTO.getUsername(); // 获取账号
        String password = DigestUtils.md5DigestAsHex(employeeLoginDTO.getPassword().getBytes()); // 获取密码 并对密码md5加密
        log.info(password + "密码是");
        log.info(username + "账户是");
        // 查询数据
        Employee employee = employeeMapper.getByUsername(username);
        if(employee.getUsername() == null){ // 账号不存在
            throw new AccountLockedException(MessageConstant.USER_NOT_LOGIN);
        }
        // 如果能走下来说明账号存在的
        // 如果密码 不相同 则 抛出异常

        if(!password.equals(employee.getPassword())){
            throw new PasswordErrorException(MessageConstant.PASSWORD_ERROR);
        }
        if(StatusConstant.DISABLE.equals(employee.getStatus()) ){
            // 账号被锁定
            throw new AccountNotFoundException(MessageConstant.ACCOUNT_NOT_FOUND);
        }
        log.info("数据信息：{}", employee);
        return  employee;
    }

    @Override
    // 新增员工
    public void save(EmployeeDTO employeeDTO) {
        Employee employee = new Employee(); // 创建一个 employee 员工对象
        BeanUtils.copyProperties(employeeDTO, employee); // 复制 前端传来的JSON对象
        // 设置员工账号状态
        employee.setStatus(StatusConstant.ENABLE);

        // 将密码转换为 md5
        employee.setPassword(DigestUtils.md5DigestAsHex(PasswordConstant.DEFAULT_PASSWORD.getBytes())); // 设置默认密码

        // 设置用户创建时间和修改时间
        employee.setCreateTime(LocalDateTime.now());
        employee.setUpdateTime(LocalDateTime.now());
        // 获取创建人员工id
        Long employeeId = BaseContext.getCurrentId();
        // 设置创建人
        employee.setCreateUser(employeeId);
        employee.setUpdateUser(employeeId);

        employeeMapper.insert(employee);
    }

    @Override
    public void pageEmployeeQuery(EmployeePageQueryDTO employeePageQueryDTO) {

    }


//    public PageResult pageEmployeeQuery(EmployeePageQueryDTO employeePageQueryDTO) {
//        int page = employeePageQueryDTO.getPage();
//        int pageSize = employeePageQueryDTO.getPageSize();
//        PageHelper.startPage(page , pageSize); // 动态拼接 limit
//        Page<Employee> page =  employeeMapper.pageEmployeeQuery(employeePageQueryDTO);
//        Long total = page.getTotal();
//        List<Employee> records = page.getResult();
//        return new PageResult(total, records);
//    }

}
